package com.daocao.ds.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.daocao.ds.R;
import com.daocao.ds.api.Api;
import com.daocao.ds.api.bean.RuleSaveBean;
import com.daocao.ds.api.model.KeyValueModel;
import com.daocao.ds.api.model.RuleListModel;
import com.daocao.ds.base.BaseActivity;
import com.daocao.ds.constant.Const;
import com.daocao.ds.event.QuaSetSuccessEvent;
import com.daocao.ds.util.Network;
import com.daocao.ds.util.RxLifeUtil;
import com.daocao.ds.util.ToastUtil;
import com.daocao.ds.util.UI;
import com.daocao.ds.util.Util;
import com.daocao.ds.view.SelectMenuDilaog;
import com.daocao.ds.view.V19FrameLayout;
import com.raizlabs.android.dbflow.sql.language.Condition;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class SnatchingModeSettingActivity extends BaseActivity {

    @BindView(R.id.txt_title)
    TextView txtTitle;

    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.btn_submit)
    SuperButton btnSubmit;
    @BindView(R.id.txt_level)
    TextView txtLevel;
    @BindView(R.id.btn_level)
    LinearLayout btnLevel;
    @BindView(R.id.txt_amount)
    TextView txtAmount;
    @BindView(R.id.btn_amount)
    LinearLayout btnAmount;
    @BindView(R.id.txt_limit_days)
    TextView txtLimitDays;
    @BindView(R.id.btn_limit_days)
    LinearLayout btnLimitDays;
    @BindView(R.id.txt_income_range)
    TextView txtIncomeRange;
    @BindView(R.id.btn_income_range)
    LinearLayout btnIncomeRange;
    @BindView(R.id.txt_socialsecurity)
    TextView txtSocialsecurity;
    @BindView(R.id.btn_socialsecurity)
    LinearLayout btnSocialsecurity;
    @BindView(R.id.txt_housefund)
    TextView txtHousefund;
    @BindView(R.id.btn_housefund)
    LinearLayout btnHousefund;
    @BindView(R.id.txt_house)
    TextView txtHouse;
    @BindView(R.id.btn_house)
    LinearLayout btnHouse;
    @BindView(R.id.txt_car)
    TextView txtCar;
    @BindView(R.id.btn_car)
    LinearLayout btnCar;

    private Unbinder unbiner;
    private SelectMenuDilaog selectMenu;
    private RuleListModel.DatasBean totalData;
    private RuleSaveBean submitBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_snatching_mode_setting);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
        EventBus.getDefault().register(this);
    }

    private void init() {
        submitBean = new RuleSaveBean();
        txtTitle.setText("设置抢单模式");
        selectMenu = new SelectMenuDilaog(this);
        selectMenu.setOnChooseListener(new SelectMenuDilaog.OnChooseListener() {
            @Override
            public void onSuccess(int poi, KeyValueModel text, String menuType) {
                switch (menuType) {
                    case Const.SnatchKEY.level:
                        txtLevel.setText(text.value);
                        submitBean.level = text.key;
                        break;
                    case Const.SnatchKEY.amount:
                        txtAmount.setText(text.value);
                        submitBean.amount = text.key;
                        break;
                    case Const.SnatchKEY.limit_days:
                        txtLimitDays.setText(text.value);
                        submitBean.limit_days = text.key;
                        break;
                    case Const.SnatchKEY.income_range:
                        txtIncomeRange.setText(text.value);
                        submitBean.income_range = text.key;
                        break;
                    case Const.SnatchKEY.socialsecurity:
                        txtSocialsecurity.setText(text.value);
                        submitBean.socialsecurity = text.key;
                        break;
                    case Const.SnatchKEY.housefund:
                        txtHousefund.setText(text.value);
                        submitBean.housefund = text.key;
                        break;
                    case Const.SnatchKEY.house:
                        txtHouse.setText(text.value);
                        submitBean.house = text.key;
                        break;
                    case Const.SnatchKEY.car:
                        txtCar.setText(text.value);
                        submitBean.car = text.key;
                        break;
                }
            }
        });
        getRulesList();
    }

    public void getRulesList() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        loadingDialog.show();

        Api.api_service.getRulesList().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    totalData = result.datas;
                });
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(QuaSetSuccessEvent event) {
        finish();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_back, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_submit:
                Intent intent = new Intent(this, QuataSettingActivity.class);
                intent.putExtra("data", submitBean);
                intent.putExtra("small_horn", totalData.small_horn);
                intent.putExtra("notice", totalData.notice);
                startActivity(intent);
                overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                break;
        }
    }

    @OnClick({R.id.btn_level, R.id.btn_amount, R.id.btn_limit_days, R.id.btn_income_range, R.id.btn_socialsecurity, R.id.btn_housefund, R.id.btn_house, R.id.btn_car})
    public void onViewClicked1(View view) {
        if (UI.isFastClick()) {
            return;
        }
        if (totalData == null) {
            return;
        }
        switch (view.getId()) {
            case R.id.btn_level:
                selectMenu.setData(Const.SnatchKEY.level, totalData.level);
                break;
            case R.id.btn_amount:
                selectMenu.setData(Const.SnatchKEY.amount, totalData.amount);
                break;
            case R.id.btn_limit_days:
                selectMenu.setData(Const.SnatchKEY.limit_days, totalData.limit_days);
                break;
            case R.id.btn_income_range:
                selectMenu.setData(Const.SnatchKEY.income_range, totalData.income_range);
                break;
            case R.id.btn_socialsecurity:
                selectMenu.setData(Const.SnatchKEY.socialsecurity, totalData.socialsecurity);
                break;
            case R.id.btn_housefund:
                selectMenu.setData(Const.SnatchKEY.housefund, totalData.housefund);
                break;
            case R.id.btn_house:
                selectMenu.setData(Const.SnatchKEY.house, totalData.house);
                break;
            case R.id.btn_car:
                selectMenu.setData(Const.SnatchKEY.car, totalData.car);
                break;
        }
        selectMenu.show();
    }
}
